package drone.basis.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

import org.springframework.web.multipart.MultipartFile;

public class FileUtil {

	/**
	 * 覆盖写入文件
	 */
	public static File create(String filePath, String fileName) throws Exception {
		if (!new File(filePath).isDirectory()) {
			new File(filePath).mkdirs();
		}
		File file = new File(filePath, fileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		return file;
	}

	/**
	 * 覆盖写入文件
	 */
	public static void write(String filePath, String fileName, String fileInfo) throws Exception {
		if (!new File(filePath).isDirectory()) {
			new File(filePath).mkdirs();
		}
		File file = new File(filePath, fileName);
		write(file, fileInfo);
	}

	/**
	 * 覆盖写入文件
	 */
	public static void write(File file, String fileInfo) throws Exception {
		if (!file.exists()) {
			file.getParentFile().mkdirs();
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(file, false));
		bw.write(fileInfo);
		bw.flush();
		bw.close();
	}

	/**
	 * 追加写入文件
	 */
	public static void append(String filePath, String fileName, String fileInfo) throws Exception {
		if (!new File(filePath).isDirectory()) {
			new File(filePath).mkdirs();
		}
		File file = new File(filePath, fileName);
		append(file, fileInfo);
	}

	/**
	 * 追加写入文件
	 */
	public static void append(File file, String fileInfo) throws Exception {
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
		bw.write(fileInfo);
		bw.flush();
		bw.close();
	}

	/**
	 * 读取文件返回字符串
	 */
	public static String read(String filePath, String fileName) throws Exception {
		if (!new File(filePath).isDirectory()) {
			new File(filePath).mkdirs();
		}
		File file = new File(filePath, fileName);
		return read(file);
	}

	/**
	 * 读取文件返回字符串
	 */
	public static String read(File file) throws Exception {
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String line = null;
		while (!Objects.isNull(line = br.readLine())) {
			sb.append(line + System.lineSeparator());
		}
		br.close();
		return sb.toString();
	}

	/**
	 * 得到图片字节流 数组大小
	 */
	public static byte[] readStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		outStream.close();
		inStream.close();
		return outStream.toByteArray();
	}

	/**
	 * 将文件转换成Byte数组
	 */
	public static byte[] getBytesByFile(File file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			byte[] data = bos.toByteArray();
			bos.close();
			return data;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * MultipartFile转File
	 */
	public static File transfer(MultipartFile param) {
		if (!param.isEmpty()) {
			File file = null;
			try {
				InputStream in = param.getInputStream();
				file = new File(param.getOriginalFilename());
				OutputStream out = new FileOutputStream(file);
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
					out.write(buffer, 0, bytesRead);
				}
				in.close();
				out.close();
				return file;
			} catch (Exception e) {
				e.printStackTrace();
				return file;
			}
		}
		return null;
	}

	/**
	 * 将InputStream写入到File中
	 *
	 */
	public static void inputstreamtofile(InputStream ins, File file) throws IOException {
		OutputStream os = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		os.close();
		ins.close();
	}

	public static String filetohex(File file) {
		byte[] bs = FileUtil.getBytesByFile(file);
		String s = HexUtil.bytes2Hex(bs);

		StringBuffer sb = new StringBuffer();
		sb.append("\"");
		int line = 0;
		for (byte b : HexUtil.hex2Bytes(s)) {
			if (line % 50 == 0&&line!=0) {
				sb.append("/" + System.lineSeparator() );
			}
			sb.append(HexUtil.byte2Hex(b));
			line++;
		}
		sb.append("\"");
		return sb.toString();
	}
}
